Skip to content

mmc: Fix MMC request issuing race condition to prevent stall#7486

Merged
P33M merged 1 commit into
raspberrypi:rpi-6.18.yfrom
TheBrokenPipe:emmc-fix
Jul 14, 2026
Merged

mmc: Fix MMC request issuing race condition to prevent stall#7486
P33M merged 1 commit into
raspberrypi:rpi-6.18.yfrom
TheBrokenPipe:emmc-fix

Conversation

@TheBrokenPipe

Copy link
Copy Markdown
Contributor

Posted write tracking introduced in the commit below referenced the request in the submission path after it had been issued, racing with re-use of the request and potentially causing underflow of the pending write count. The count is signed but was compared against the unsigned posted write limit, so such an underflow would wrap to a large value and wrongly throttle further writes (hang entire system).

Fixes: e6c1e86 ("mmc: restrict posted write counts for SD cards in CQ mode")

This fix is the same as ef0aded, but for the request issuing path. It additionally fixes the unsigned vs signed integer comparison, which actually causes stalls when pending_writes does underflow to negative for whatever reason as it is promoted to unsigned 0xFFF... or some huge unsigned integer, causing the comparison to always be true and BLK_STS_RESOURCE always returned.

@P33M

P33M commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Have you experienced a situation with an A2 SD card where you got such a hang, and this was likely to be the cause?

@TheBrokenPipe

Copy link
Copy Markdown
Contributor Author

Have you experienced a situation with an A2 SD card where you got such a hang, and this was likely to be the cause?

Hi, sorry, I have not. I have only ever tested on eMMCs on a CM5 device, which this issue can be easily reproduced running stress-ng's directory test.

@P33M

P33M commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Please provide the stress-ng commandline that provokes the bug.

@TheBrokenPipe

TheBrokenPipe commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

Please provide the stress-ng commandline that provokes the bug.

run_disk_stress_ng.sh
I've been testing by running sudo DIR_WORKER_COUNT=4 ./run_disk_stress_ng.sh mmcblk0 60 with this script and stress-ng in the same directory. Without the patch it has a good (>80%) chance of hanging.

@P33M

P33M commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

That script has been running for over an hour with no fails - CM5, 16GB eMMC, 2GB RAM, Pi OS Trixie with 6.18.34+rpt-rpi-2712 (latest apt kernel), stress-ng, version 0.19.02 (gcc 14.2.0, aarch64 Linux 6.18.34+rpt-rpi-2712) . Is there something I'm missing?

@TheBrokenPipe

Copy link
Copy Markdown
Contributor Author

That script has been running for over an hour with no fails - CM5, 16GB eMMC, 2GB RAM, Pi OS Trixie with 6.18.34+rpt-rpi-2712 (latest apt kernel), stress-ng, version 0.19.02 (gcc 14.2.0, aarch64 Linux 6.18.34+rpt-rpi-2712) . Is there something I'm missing?

Oh I forgot, you need to enable discard to really stress the queue, my bad. Try something like sudo mount -o remount,discard / or replace defaults,noatime with discard in /etc/fstab.

@P33M

P33M commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Mounting as discard is very not-recommended as it kills performance (and flash endurance), but does provoke the bug in short order. It is underflowing, this patch fixes that.

I'd prefer declaring both variables as ints and explicitly warning on underflow in case there's another situation that can cause it. Also for consistency use the bool in the test for BLK_STS_RESOURCE.

I think I've pushed the right buttons to update your branch, please review/test.

@TheBrokenPipe

TheBrokenPipe commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

I think I've pushed the right buttons to update your branch, please review/test.

Hi, I have reviewed and retested, they look good and still work. Thank you for the changes!

Posted write tracking introduced in the commit below referenced the
request in the submission path after it had been issued, racing with
re-use of the request and potentially causing underflow of the pending
write count. The count is signed but was compared against the unsigned
posted write limit, so such an underflow would wrap to a large value
and wrongly throttle further writes (hang entire system).

Fixes: e6c1e86 ("mmc: restrict posted write counts for SD cards in CQ mode")

Co-developed-by: Jonathan Bell <jonathan@raspberrypi.com>
Signed-off-by: Jonathan Bell <jonathan@raspberrypi.com>
Signed-off-by: Yufeng Gao <yufeng.gao@canonical.com>
@P33M P33M merged commit 66d4664 into raspberrypi:rpi-6.18.y Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants